<?php

namespace App\Http\Requests\Api;

class SettingRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(): array
    {
        switch ($this->route()->getName()) {
            case 'settings.store':
                return [
                    'key' => 'required|max:32|unique:settings',
                    'label' => 'nullable|max:32',
                    'value' => 'required',
                ];

            case 'settings.update':
                $id = $this->segment(4); // 排除掉当前配置
                return [
                    'key' => 'required|max:32|unique:settings,key,' . $id,
                    'label' => 'nullable|max:32',
                    'value' => 'required',
                ];

            default:
                ;
        }
    }

    /**
     * 验证消息
     * @return string[]
     */
    public function messages(): array
    {
        return [
            'key.required' => '配置项不能为空',
            'key.max' => '配置项最大长度为 32 个字符',
            'key.unique' => '配置项已经存在',
            'label.max' => '配置描述最大长度为 32 个字符',
            'value.required' => '配置值不能为空',
        ];
    }
}
